package controllers

import (
	"github.com/kataras/iris"
	"github.com/kataras/iris/mvc"
	"tizhi_front/model"
	"tizhi_front/nspel"
	"tizhi_front/response"
	"tizhi_front/utils"
)

type PrescriptionController struct {
	Ctx iris.Context
}

func (c *PrescriptionController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/list/{offset:int min(0) max(5)}", "GetPrescriptionList", beforeCommonToken)
	b.Handle("POST", "/detail/{id:long}", "GetPrescriptionDetail", beforeCommonToken)
}

// @Title Prescription List
// @Description 根据个人的测试数据，推荐出运动处方
// @Tags prescription
// @Param	offset path int64	true "offset for list"
// @Param	body body response.TokenInfo true " "
// @Success 0	{object} response.PrescriptionList
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /prescription/list/{offset} [post]
func (c *PrescriptionController) GetPrescriptionList(offset int) response.JsonResponse {
	uid, err := c.Ctx.Values().GetInt64("uid")
	if err != nil {
		return response.InvalidParameters
	}
	prescription, err := utils.GetPrescriptionList(uid)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	if offset >= len(prescription) {
		return response.JsonResponse{
			ErrCode: 0,
			ErrInfo: "",
			Data: response.PrescriptionList{
				PerRequestCount:  5,
				TotalCount:       int64(len(prescription)),
				Offset:           int64(offset),
				PrescriptionList: []response.PrescriptionListInfo{},
			},
		}
	} else {
		res := response.PrescriptionList{
			PerRequestCount:  5,
			TotalCount:       int64(len(prescription)),
			Offset:           int64(offset),
			PrescriptionList: []response.PrescriptionListInfo{},
		}
		for i := offset; i < offset+5 && i < len(prescription); i++ {
			res.PrescriptionList = append(res.PrescriptionList, response.PrescriptionListInfo{
				Id:      prescription[i].Id,
				Title:   prescription[i].Name,
				Type:    int(prescription[i].LabelId),
				PicUrl:  prescription[i].PicUrl,
				Actions: prescription[i].Actions,
			})
		}
		return response.JsonResponse{
			ErrCode: 0,
			ErrInfo: "",
			Data:    res,
		}
	}
}

// @Title Prescription Detail
// @Description 处方详情
// @Tags prescription
// @Param	id path int64	true "id for prescription"
// @Param	body body response.TokenInfo true " "
// @Success 0	{object}	response.PrescriptionDetail
// @Failure 1 {string} string "用户未登录或者登陆已过期"
// @Failure 2 {string} string "系统错误，获取失败，具体error信息为系统报错信息"
// @router /prescription/detail/{id} [post]
func (c *PrescriptionController) GetPrescriptionDetail(id int64) response.JsonResponse {
	prescription := model.Prescription1{}
	err := prescription.SelectById(id)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	prescriptionItem := model.PrescriptionItemE{}
	prescriptionItems, err := prescriptionItem.SelectByPrescriptionIdOrderByOrderAsc(id)
	if err != nil {
		nspel.Error(err)
		return response.JsonResponse{
			ErrCode: 2,
			ErrInfo: err.Error(),
			Data:    "",
		}
	}
	res := response.PrescriptionDetail{
		PrescriptionListInfo: response.PrescriptionListInfo{
			Id:      id,
			Title:   prescription.Name,
			Type:    int(prescription.LabelId),
			PicUrl:  prescription.PicUrl,
			Actions: prescription.Actions,
		},
	}
	res.Actions = make([]response.ActionInfo, len(prescriptionItems))
	for i := 0; i < len(res.Actions); i++ {
		res.Actions[i] = response.ActionInfo{
			ActionName:   prescriptionItems[i].Name,
			Title:        prescriptionItems[i].Title,
			PracticeNum:  int(prescriptionItems[i].PracticeNum),
			PracticeTime: int(prescriptionItems[i].PracticeTime),
			IntervalTime: int(prescriptionItems[i].IntervalTime),
			Content:      prescriptionItems[i].Content,
		}
	}
	return response.JsonResponse{
		ErrCode: 0,
		ErrInfo: "",
		Data:    res,
	}
}
